<?php

/**
 * 
 * @author 陈德本
 *
 */
class newsController extends Controller {

    public $initphp_list = array(
        'index',
        'content'
    );
    private $conf;
    public function __construct(){
        parent::__construct();
        $this->conf=InitPHP::getConfig();
        $this->view->assign("pro_cat", $this->getProduct()->getMenu());
        $this->view->assign("nav", $this->getNav()->getNav(true,1));
        $this->view->assign("slide", $this->getSlide()->getAll());
        $this->view->assign("link", $this->getLink());
        $this->view->assign("category", $this->getCatgory()->getCatgory());
    }
    public function index() {
        $id=$this->controller->get_get('id');
        $id=$id?:1;
        $cat_data=$this->getCatgory()->getOne($id);
        $url="/".$cat_data['url'];
        $pager = $this->getLibrary('pager');
        $page = $this->controller->get_gp("page");
        $page=$page>0?$page:1;
        $pernum = 20;
        $start = $pernum * ($page - 1);
        $news_data=$this->getNews()->getList($id, $start, $pernum);
        $page_html = $pager->pager($news_data[1], $pernum, $url, $page, true); // 最后一个参数为true则使用默认样式
        $this->view->assign("title", $cat_data['seo_title']);
        $this->view->assign("keywords", $cat_data['keyword']);
        $this->view->assign("description", $cat_data['descript']);
        $this->view->assign("pager", $page_html);
        $this->view->assign("cat_data", $cat_data);
        $this->view->assign("news_data", $news_data[0]);
        $this->view->set_tpl("home/news/list"); // 中间的模板
        $this->view->display();
    }
    public function content(){
        $id=$this->controller->get_get('id');
        $id=$id?:1;
        $data=$this->getNews()->getOne($id);
        if (!$data){
            JsMessage("文章不存在！","/home");
            die();
        }
        $this->getNews()->incViews($id, $data['views']);
        $this->view->assign("title", $data['seo_title']);
        $this->view->assign("keywords", $data['keyword']);
        $this->view->assign("description", $data['descript']);
        $this->view->assign("data", $data);
        $cat_data=$this->getCatgory()->getOne($data['pid']);
        $this->view->assign("pn", $this->getNews()->getPrevAndNext($id));
        $this->view->assign("cat", $cat_data);
        $this->view->set_tpl("home/news/content");
        $this->view->display();
    }
    /**
     * 
     * @return newsService
     */
    private function getNews(){
        return InitPHP::getService("news");
    }
    /**
     *
     * @return navService
     */
    private function getNav(){
        return InitPHP::getService("nav");
    }
    
    /**
     * 
     * @return catgoryService
     */
    private function getCatgory(){
        return InitPHP::getService("catgory");
    }
    /**
     *
     * @return productService
     */
    private function getProduct(){
        return InitPHP::getService("product");
    }
    /**
     *
     * @return slideService
     */
    private function getSlide(){
        return InitPHP::getService("slide");
    }
    private function getLink(){
        return InitPHP::getDao("link")->getAll();
    }

}